package com.example.message;

import java.util.function.*;

//函数编程
public class FunctionExample {
    public static void main(String[] args) {
        Supplier<Product> supplier = Product::new;
        Product product = supplier.get();

        //类名+静态方法  一个输入T 没有输出
        Consumer<Product> consumer1 = Product-> com.example.message.Product.nameOf(product);//lambda
        consumer1.accept(product);
        Consumer<Product>  consumer = Product::nameOf;//方法引用
        consumer.accept(product);

    }

    public static void r() {
        // Supplier 没有输入 只有输出
        //无参数构造函数
        Supplier<Product> supplier = Product::new;
        System.out.println("创建新对象:" + supplier.get());

        Product product = supplier.get();
        product.setStock(10);

        Supplier<Integer> supplier1 = product::getStock;
        System.out.println("剩余库存:" + supplier1.get());
    }

    // Predicate 一个输入T, 一个输出 Boolean
    public static void f() {
        Supplier<Product> supplier = Product::new;
        Product product = supplier.get();

        Predicate<Integer> predicate = product::isEnough;//方法引用
        System.out.println("库存是否足够：" + predicate.test(100));
    }

    // Funtion<T,R> 一个输入 一个输出
    public static void functions() {
        Supplier<Product> supplier = Product::new;
        Product product = supplier.get();
        product.setStock(10);

        //对象+方法  一个输入T 一个输出R
        Function<Integer, Integer> function = product::reduceStock;
        System.out.println("剩余库存：" + function.apply(12));

        //带参数的构造函数
        Function<Integer, Product> function1 = Product::new;
        System.out.println("新对象:" + function1.apply(200));
    }

    //UnaryOperator 一元操作符 输入输出都是T
    public static void lambda() {
        Supplier<Product> supplier = Product::new;
        Product product = supplier.get();
        product.setStock(100);

        //一元操作符  输入和输出T
        UnaryOperator<Integer> integerUnaryOperator = product::reduceStock;
        System.out.println("剩余库存：" + integerUnaryOperator.apply(20));
        IntUnaryOperator intUnaryOperator = product::reduceStock;
        System.out.println("剩余库存：" + intUnaryOperator.applyAsInt(30));
    }

    public static void g() {
        Supplier<Product> supplier = Product::new;
        Product product = supplier.get();
        product.setStock(100);

        //BiFunction 二元操作符 两个输入<T,U> 一个输出 类名+方法
        //BiFunction<Product, Integer, Integer> binaryOperator = Product::reduceStock;
        //System.out.println("剩余库存(BiFunction)：" + binaryOperator.apply(product, 10));

        //BinaryOperator 二元操作符 ,二个输入 一个输出
        //BinaryOperator binaryOperator1=(x,y)->product.reduceStock(x,y);
        BinaryOperator<Integer> binaryOperator1 = product::reduceStock;
        System.out.println(" 剩余库存(BinaryOperator)：" +binaryOperator1.apply(product.getStock(),10));
    }
}
